using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace Astro.Core
{
	/// <summary>
	/// Defines a single custom form. This object is stored in the ObjectStore
	/// </summary>
	[Serializable]
    public class CustomPostFormSettings : CustomFormSettings
	{
		public static readonly string DefaultCustomFormName = "Default::Custom::Post::Form";
        public static readonly string CustomFormName = "CustomPostFormSettings-";

		public int CategoryId;
		public int FormCategoryId;

		/// <summary>
		/// Returns a named custom form. The default post form is named "post".
		/// </summary>
		public static CustomPostFormSettings Get(int categoryId)
		{
			return Get(categoryId, true);
		}

		public static CustomPostFormSettings Get(int categoryId, bool processFormInheritance)
		{
			if (categoryId > 0)
				return Get(new CategoryController().GetCachedCategory(categoryId, false), processFormInheritance);
			else
				return Get();
		}

		public static CustomPostFormSettings Get(Category category)
		{
			return Get(category, true);
		}

		public static CustomPostFormSettings Get(Category category, bool processFormInheritance)
		{
			if (category == null)
				return Get();

			CustomPostFormSettings cfs = ObjectManager.Get<CustomPostFormSettings>(CustomFormName + category.Id);
			cfs.FormCategoryId = category.Id;

			if (processFormInheritance)
			{
				CustomPostFormSettings combinedCfs = new CustomPostFormSettings();
				combinedCfs.Name = category.Name;
				combinedCfs.FormCategoryId = category.Id;
				combinedCfs.IsNew = cfs.IsNew;
				combinedCfs.Fields = new List<CustomField>();

				if (cfs.Fields != null)
				{
					foreach (CustomField field in cfs.Fields)
					{
						combinedCfs.Fields.Add(field);
					}
				}

				CustomPostFormSettings cfs2;
				if (category.ParentId > 0)
				{
					cfs2 = ObjectManager.Get<CustomPostFormSettings>(CustomFormName + category.ParentId);

					if (cfs2.Fields != null)
					{
						foreach (CustomField field in cfs2.Fields)
						{
							combinedCfs.Fields.Add(field);
						}
					}

					if (combinedCfs.IsNew)
					{
						combinedCfs.FormCategoryId = category.ParentId;
						combinedCfs.IsNew = false;
					}
				}

				cfs2 = Get();
				if (cfs2.Fields != null)
				{
					foreach (CustomField field in cfs2.Fields)
					{
						combinedCfs.Fields.Add(field);
					}
				}

				if (combinedCfs.IsNew)
				{
					combinedCfs.FormCategoryId = -1;
					combinedCfs.IsNew = false;
				}

				cfs = combinedCfs;
			}

			cfs.CategoryId = category.Id;

			return cfs;
		}

		public static CustomPostFormSettings Get()
		{
			CustomPostFormSettings cfs = ObjectManager.Get<CustomPostFormSettings>(DefaultCustomFormName);
			cfs.FormCategoryId = -1;
			cfs.CategoryId = -1;

			return cfs;
		}

		public void Save()
		{
			if (string.IsNullOrEmpty(Name))
				throw new Exception("The name of the custom form cannot be null");

			this.IsNew = false;
			if (CategoryId == -1)
				ObjectManager.Save(this, DefaultCustomFormName);
			else
				ObjectManager.Save(this, CustomFormName + CategoryId.ToString());
		}
	}
}
